package com.hht.toolanduilibs.wb;

import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;

import androidx.core.content.FileProvider;

import java.io.File;

/**
 * Author:chen'hu
 * Time:2023/12/7 15:27
 * Description:This is WhiteBoardUtils
 */
public class WhiteBoardUtils {

    private Context mContext;
    private PlatformsUtils platformsUtils;

    public WhiteBoardUtils(Context context) {
        mContext = context;
        platformsUtils = new PlatformsUtils(context);
    }


    //打开白板
    public void startWhiteBoard() {
        final Intent intent = new Intent();
        if (platformsUtils.isUseOtherWb()) {
            intent.setAction(Intent.ACTION_MAIN);
            intent.setComponent(new ComponentName("com.ist.whiteboard", "com.ist.whiteboard.MainActivity"));
        } else {
            intent.setAction("android.hht.action.whiteboard");
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("extraEndMeeting", false);
        intent.putExtra("extraHideApp", false);
        try {
            mContext.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
            if (!openBeijingWhiteBoard()) {
                if (!openMoJeWhiteBoard()) {
                    if (!openLgWhiteBoard()) {
                        openOptomaWhiteBoard();
                    }
                }
            }
        }

    }

    //打开批注
    public void startAnnotation() {
        if (platformsUtils.isUseOtherWb()) {
            openIstAnnotation();
        } else {
            if(!openOldWbAnnotation()){
                if(!openWbAnnotation()){
                    openBeijingAnnotation();
                    openAnnotation();
                    openLgAnnotation();
                    openOptomaAnnotation();
                }
            }
        }
    }

    //安卓11及以下的OEM全用老批注，安卓13就用金总的新批注
    private boolean openOldWbAnnotation() {
        boolean isOpenSuccess = true;
        Intent intent = new Intent();
        intent.putExtra("extraAnnotationMode", true);
        intent.setComponent(new ComponentName("tw.com.hitevision.whiteboard", "tw.com.hitevision.whiteboard.android.MainActivity"));
        intent.putExtra("extraEndMeeting", false);
        intent.putExtra("extraHideApp", false);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            mContext.startActivity(intent);
        } catch (Exception e) {
            isOpenSuccess = false;
        }
        return isOpenSuccess;
    }

    //安卓13就用金总的新批注
    private boolean openWbAnnotation() {
        boolean isOpenSuccess = true;
        Intent intent = new Intent();
        intent.putExtra("extraAnnotationMode", true);
        intent.setComponent(new ComponentName("tw.com.newline.annotation", "tw.com.newline.annotation.android.MainActivity"));
        intent.putExtra("extraEndMeeting", false);
        intent.putExtra("extraHideApp", false);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            mContext.startActivity(intent);
        } catch (Exception e) {
            isOpenSuccess = false;
        }
        return isOpenSuccess;
    }

    private void openIstAnnotation() {
        Intent intent = new Intent("com.ist.START_ANNOTATION");
        intent.setPackage("com.ist.whiteboard");
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        //sendBroadcast(intent); //主用户下这样发送可以，子用户下打不开，需要用用底下的方法
        mContext.sendBroadcast(intent);
    }


    //金总的批注，最新的
    private void openAnnotation() {
        //don't ask me ,i don't know anything
        final Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.hht.annotation", "com.hht.annotation.AnnotationService"));
        mContext.startService(intent);
    }

    //金总的批注，最新的
    private void openOptomaAnnotation() {
        //don't ask me ,i don't know anything
        final Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.optoma.annotation", "com.optoma.annotation.AnnotationService"));
        mContext.startService(intent);
    }

    //LG的批注，最新的
    private void openLgAnnotation() {
        //don't ask me ,i don't know anything
        final Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.xbh.whiteboard", "com.xbh.whiteboard.annotation.service.SuspendService"));
        mContext.startService(intent);
    }

    //北京的批注，很老的
    private void openBeijingAnnotation() {
        //don't ask me ,i don't know anything
        Intent intent = new Intent();
        intent.setClassName("com.android.hht.annotate", "com.android.hht.annotate.service.AnnotateService");
        mContext.startService(intent);
    }


    //北京的白板（很老的项目，基本不用了）
    private boolean openBeijingWhiteBoard() {
        //don't ask me ,i don't know anything
        boolean isOpen = true;
        try {
            PackageManager packageManager = mContext.getPackageManager();
            Intent intent = packageManager.getLaunchIntentForPackage("com.android.hht.whiteboard");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION);
            mContext.startActivity(intent);
            mContext.sendBroadcast(new Intent("com.hht.floatbar.whiteboard"));
        } catch (Exception e) {
            e.printStackTrace();
            isOpen = false;
        }
        return isOpen;
    }

    //MOJE与IST_30_19C的白板（很老的项目，基本不用了）
    private boolean openMoJeWhiteBoard() {
        boolean isOpen = true;
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("tw.com.hitevision.whiteboard", "tw.com.hitevision.whiteboard.android.MainActivity"));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            isOpen = false;
        }
        return isOpen;
    }

    //M24前期用LG的白板
    private boolean openLgWhiteBoard() {
        boolean isOpen = true;
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.xbh.whiteboard", "com.xbh.whiteboard.LaunchActivity"));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            isOpen = false;
        }
        return isOpen;
    }

    private boolean openOptomaWhiteBoard() {
        boolean isOpen = true;
        try {
            Intent intent = new Intent();
            ComponentName componentName = new ComponentName("com.optoma.whiteboard",
                    "com.optoma.whiteboard.cb.MainActivity");
            intent.setComponent(componentName);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            isOpen = false;
        }
        return isOpen;
    }


    //把图片传到白板中
    public void openWhiteBoardAndUir(File file) {
        final Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            String key = platformsUtils.getCustomerPrdID();
            Log.d("Hogan", "贴至白板 CustomerPrdID=" + key);
            switch (key) {
                case PlatformsUtils.IST_30_CPLUS:
                    intent.setAction("com.hht.fileToWhiteboard");
                    intent.putExtra("filePath", file.getPath());
                    intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                    mContext.sendBroadcast(intent);
                    break;
                case PlatformsUtils.IST_30_19C:
                case PlatformsUtils.IST_30_MOJE:
                    intent.setComponent(new ComponentName("tw.com.hitevision.whiteboard",
                            "tw.com.hitevision.whiteboard.android.MainActivity"));
                    intent.putExtra("filePath", file.getPath());
                    mContext.startActivity(intent);
                    break;
                default:
                    String keyPlatforms = platformsUtils.getHongHeKeyPlatforms();
                    Log.d("Hogan", "贴至白板 keyPlatforms=" + keyPlatforms);
                    switch (keyPlatforms) {
                        //TODO M22是optomo的，他们的白板不是鸿合的，22A是中性的，白板是用鸿合OEM的
                        case PlatformsUtils.HHT_KEY_PLATFORMS_M22:
                            ComponentName componentName = new ComponentName("com.optoma.whiteboard",
                                    "com.optoma.whiteboard.cb.MainActivity");
                            intent.setComponent(componentName);
                            Uri pathUri = Uri.parse(file.getPath());
                            intent.setDataAndType(pathUri, "image/*");
                            mContext.startActivity(intent);
                            break;
                        case PlatformsUtils.HHT_KEY_PLATFORMS_22A:
                            boolean isUseOther = platformsUtils.isUseOtherWb();
                            if (isUseOther) {
                                intent.setClassName("com.ist.whiteboard", "com.ist.whiteboard.MainActivity");
                                Uri uri = Uri.fromFile(file);
                                intent.setData(uri);
                                intent.putExtra("position", new int[]{500, 500});
                                mContext.startActivity(intent);
                            } else {
                                intent.setAction("android.hht.action.whiteboard");
                                intent.putExtra("filePath", file.getPath());
                                mContext.startActivity(intent);
                            }
                            break;
                        case PlatformsUtils.HHT_KEY_PLATFORMS_RS:
                            intent.setComponent(new ComponentName("tw.com.newline.whiteboard",
                                    "tw.com.newline.whiteboard.android.MainActivity"));
                            intent.putExtra("filePath", file.getPath());
                            mContext.startActivity(intent);
                            break;
                        default:
                            String sysKeyPlatforms = platformsUtils.getKeyPlatforms();
                            Log.d("Hogan", "贴至白板 标准的sysKeyPlatforms=" + sysKeyPlatforms);
                            //TODO M24暂时用LG的白板
                            if (PlatformsUtils.HHT_KEY_PLATFORMS_MT9679_NORMAL.equals(sysKeyPlatforms)) {
                                intent.setAction("com.xbh.intent.action.INSERT_IMAGE_TO_WHITEBOARD");
                                Uri uri = FileProvider.getUriForFile(mContext, "com.xbh.whiteboard.fileProvider", file);
                                intent.setDataAndType(uri, "image/*");
                                mContext.startActivity(intent);
                            } else {
                                String model = platformsUtils.getModelPlatforms();
                                Log.d("Hogan", "贴至白板 model=" + model);
                                if ("RK3576".equals(model)) {
                                    ComponentName optomaComponentName = new ComponentName("com.optoma.whiteboard",
                                            "com.optoma.whiteboard.cb.MainActivity");
                                    intent.setComponent(optomaComponentName);
                                    Uri optomaPathUri = Uri.parse(file.getPath());
                                    intent.setDataAndType(optomaPathUri, "image/*");
                                    mContext.startActivity(intent);
                                } else {
                                    intent.setAction("android.hht.action.whiteboard");
                                    intent.putExtra("filePath", file.getPath());
                                    mContext.startActivity(intent);
                                }
                            }
                            break;
                    }
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
